package com.example.asuper.check.SdcardManage;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.example.asuper.check.Helper.SharePrefenceHelper;
import com.example.asuper.check.Helper.StringHelper;
import com.example.asuper.check.Model.CheckModel;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Super on 2015/12/27.
 */
public class FileService {
    private String TAG = "Sdcard";
    private Context mContext;
    private String SpliteCode;

    public FileService(Context context) {
        mContext = context;
        SpliteCode= SharePrefenceHelper.ReadSharedPreferences("splite_info","SPLITE",mContext);
    }

    public FileService() {

    }

    public boolean writeContentToSdcard(String fileName, String content) throws IOException {
        boolean result = false;
        FileOutputStream fileOutputStream = null;
        File file = new File(Environment.getExternalStorageDirectory(), fileName);
        Log.i(TAG, "filepath=>" + file.getPath());
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            Log.i(TAG, "exten" + Environment.getExternalStorageState());
            try {
                fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(content.getBytes());
                result = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
        }
        return result;
    }

    public String getFileFromSdcard(String fileName) {
        FileInputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        File file = new File(Environment.getExternalStorageDirectory(), fileName);
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            try {
                inputStream = new FileInputStream(file);
                int len = 0;
                byte[] data = new byte[1024];

                while ((len = inputStream.read(data)) != -1) {
                    byteArrayOutputStream.write(data, 0, len);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return new String(byteArrayOutputStream.toByteArray());
    }


    public ArrayList<CheckModel> getFile(String fileName) {
        ArrayList<CheckModel> checkModels = new ArrayList<CheckModel>();
        String[] columsName = null;
        String columsNames = "";
        String[] columsValue;
        CheckModel checkModel;
        boolean isFirst = true;
        FileInputStream inputStream = null;
        String FileRest = "";
        String Line = "";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        File file = new File(Environment.getExternalStorageDirectory(), fileName);
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            try {
                inputStream = new FileInputStream(file);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "GB2312"));
                while ((Line = bufferedReader.readLine()) != null) {
                    //FileRest+=Line;
                    checkModel = new CheckModel();
                    if (isFirst) {
                        columsNames = Line;
                        columsName = Line.split(SpliteCode);
                    } else {
                        columsValue = Line.split(SpliteCode);
                        checkModel.setCheckCode(columsValue[0]);
                        checkModel.setRemark(Line.replace(columsValue[0] + StringHelper.getSpliteM( SpliteCode), ""));
                        checkModel.setmCheckColums(columsNames.replace(columsName[0] +StringHelper.getSpliteM( SpliteCode), ""));
                        checkModel.setState("1");
                        checkModels.add(checkModel);
                    }
                    isFirst = false;
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return checkModels;
    }

    public boolean isExistsFile(String fileName)
    {
        File file=new File(Environment.getExternalStorageDirectory(),fileName);
        if (file.exists()){
            return true;
        }
        return false;
    }

    public boolean DeleteFile(String fileName)
    {
        File file=new File(Environment.getExternalStorageDirectory(),fileName);
        if (file.exists()){
            return file.delete();
        }
        return false;
    }
}
